package com.building18.controller;

import com.building18.annotations.RequirePermCheck;
import com.building18.handler.MyAssert;
import com.building18.handler.exception.MyException;
import com.building18.pojo.Building;
import com.building18.pojo.MyPageData;
import com.building18.pojo.Result;
import com.building18.pojo.enums.BuildingCode;
import com.building18.pojo.enums.Code;
import com.building18.service.BuildingService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/building")
@Tag(name = "楼宇管理", description = "楼宇信息的增删改查接口")
public class BuildingController {

    @Autowired
    private BuildingService buildingService;


    @GetMapping("/{pageNum}/{pageSize}")
    @Operation(summary ="查询楼宇所有信息")
    public Result<?> selectBuilding(@PathVariable int pageNum, @PathVariable int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Building> buildings = buildingService.selectBuilding();
        PageInfo<Building> buildingPageInfo = new PageInfo<>(buildings);
        MyPageData myPageData=new MyPageData(buildingPageInfo.getTotal(),buildingPageInfo.getList());
        return Result.ok(myPageData, Code.SELECT_SUCCESS.getMessage());
    }
    @PostMapping("insertBuilding")
    @Operation(summary ="添加楼宇")
    public Result<Integer> addBuilding(@RequestBody Building building){
        Integer i = buildingService.addBuilding(building);
        return Result.ok(i, Code.INSERT_FAIL.getMessage());
    }

    @DeleteMapping("deleteById/{id}")
    @Operation(summary ="根据id删除楼宇")
    public Result<Integer> deleteBuildingById(@PathVariable("id") Integer id){
        Integer num= buildingService.countHouseByBuildingId(id);
        MyAssert.error(num>0, BuildingCode.DELETE_BUILDING_FAIL.getMessage());
        Integer i = buildingService.deleteBuildingById(id);
        return Result.ok(i, Code.DELETE_SUCCESS.getMessage());
    }

    @PostMapping("deleteByIds")
    @Operation(summary ="根据id批量删除楼宇")
    public Result<Integer> deleteBuildingByIds(@RequestBody List<Integer> ids) {
        for (Integer id : ids){
            Integer num= buildingService.countHouseByBuildingId(id);
            MyAssert.error(num>0, BuildingCode.DELETE_BUILDING_FAIL.getMessage());
        }
        Integer i = buildingService.deleteBuildingByIds(ids);
        if (i>0){
        return Result.ok(i, Code.DELETE_SUCCESS.getMessage());
        }else {
            return Result.fail(Code.DELETE_FAIL.getMessage());
        }
    }

    @GetMapping("selectBuildingByName/{name}")
    @Operation(summary ="根据楼宇名称查询楼宇")
    public Result<List<Building>> selectBuildingByName(@PathVariable String name){
        List<Building> buildings = buildingService.selectBuildingByName(name);
        return Result.ok(buildings, Code.SELECT_SUCCESS.getMessage());
    }

    @PutMapping("updateBuilding")
    @Operation(summary ="修改楼宇信息")
    public Result<Integer> updateBuilding(@RequestBody Building building){
        Integer i = buildingService.updateBuilding(building);
        return Result.ok(i, Code.UPDATE_SUCCESS.getMessage());
    }

    @GetMapping("/selectBuildingIdAndName")
    @Operation(summary ="查询楼宇id和名称")
    public Result<List<Building>> selectBuildingIdAndName(){
        List<Building> buildings = buildingService.selectBuildingIdAndName();
        return Result.ok(buildings, Code.SELECT_SUCCESS.getMessage());
    }
}
